// 这是一个入口文件
const Koa = require("koa");
const app = new Koa();
const Router = require("koa-router");
const router = new Router();
const bodyParser = require("koa-bodyparser");
const UserData = [
	{
		username: "kkk",
		password: "123",
		role: "admin",
	},
];
router.get("/", ctx => {
	ctx.body = "hello 1905A";
});
// queryString
router.get("/login", ctx => {
	const { username, password } = ctx.query;
	const user = UserData.find(
		item => item.username == username && item.password == password
	);
	if (user) {
		ctx.body = {
			success: true,
			data: {
				role: user.role,
			},
		};
	}
});
// body
router.post("/register", ctx => {
	const { username, password } = ctx.request.body;
	UserData.push({ username, password, role: "editor" });
	ctx.body = {
		success: true,
		message: "注册成功",
	};
});
// params
router.get("/delete/user/:username", ctx => {
	const { username } = ctx.params;
	const ind = UserData.findIndex(item => item.username == username);
	if (ind !== -1) {
		UserData.splice(ind, 1);
	}
	ctx.body = {
		success: true,
		message: "删除成功",
	};
});
app.use(bodyParser());
app.use(router.routes()); // 注册路由
app.listen(9999, () => {
	console.log("9999 listen");
});
